//
//  DownloadedChapterViewController.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 12-12-12.
//  Copyright (c) 2012年 wzyk. All rights reserved.
//

#import "DownloadedChapterViewController.h"
#import "DownloadedChapterCell.h"
#import "PlayerViewController.h"

@interface DownloadedChapterViewController ()

@end

@implementation DownloadedChapterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self updateDisplay];
}

- (void)updateDisplay
{
    titleLabel.text = self.item_name;
    self.downloadedChapterArray = [[DownloadManager defaultManager] getDownloadedChapterByID:self.item_id];
    [downloadedChapterTableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.downloadedChapterArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DownloadedChapterCell *cell = [downloadedChapterTableView dequeueReusableCellWithIdentifier:@"DownloadedChapterCell"];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"DownloadedChapterCell" owner:self options:nil] objectAtIndex:0];
    }
    cell.chapterIndex = indexPath.row;
    cell.chapterInfo = [self.downloadedChapterArray objectAtIndex:indexPath.row];
    [cell updateDisplay];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[AudioPlayer defaultPlayer] playlist:self.downloadedChapterArray fromIndex:indexPath.row];
    
    //跳转到播放界面
    PlayerViewController *playerViewController = [[[PlayerViewController alloc] initWithNibName:@"PlayerViewController" bundle:nil] autorelease];
    UINavigationController *navController = (UINavigationController *)self.view.window.rootViewController;
    [navController pushViewController:playerViewController animated:YES];
}

- (IBAction)navBack
{
    UINavigationController *navController = (UINavigationController *)self.view.window.rootViewController;
    [navController popViewControllerAnimated:YES];
}

- (IBAction)navBackToHome
{
    UINavigationController *navController = (UINavigationController *)self.view.window.rootViewController;
    [navController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [titleLabel release];
    [downloadedChapterTableView release];
    [_item_id release];
    [_downloadedChapterArray release];
    [super dealloc];
}
- (void)viewDidUnload {
    [titleLabel release];
    titleLabel = nil;
    [downloadedChapterTableView release];
    downloadedChapterTableView = nil;
    [super viewDidUnload];
}
@end
